package UtilsApplication;

import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import ModelApplication.Sound;

public class WaveChartUtil {
    
    public static JFreeChart plotWave(Sound sound) {
        XYSeriesCollection dataset = new XYSeriesCollection();
        JFreeChart chart = null;
        
        if(sound.getChartType() == ChartTypes.FREQ_VALUE) {
            dataset.addSeries(createSeriesFreqValue( sound.getAudio() , sound.getName() ));
            chart = ChartFactory.createXYLineChart
                    ("Dzwiek - " + sound.getName(),"Częstotliwość","", dataset, PlotOrientation.VERTICAL, false,false,false);
        } else if(sound.getChartType() == ChartTypes.TIME_AMPLITUDE) {
            dataset.addSeries(createSeriesTimeAmp( sound.getAudio() , sound.getName() ));
            chart = ChartFactory.createXYLineChart
                    ("Dzwiek - " + sound.getName(),"Czas","Amplituda", dataset, PlotOrientation.VERTICAL, false,false,false);
        } else if ( sound.getChartType() == ChartTypes.VALUE_VALUE) {
            dataset.addSeries(createSeriesValueValue( sound.getAudio() , sound.getName() ));
            chart = ChartFactory.createXYLineChart
                    ("Dzwiek - " + sound.getName(),"","", dataset, PlotOrientation.VERTICAL, false,false,false);
        } else if ( sound.getChartType() == ChartTypes.VALUE_AMPLITUDE) {
            dataset.addSeries(createSeriesValueAmp( sound.getAudio() , sound.getName() ));
            chart = ChartFactory.createXYLineChart
                    ("Dzwiek - " + sound.getName(),"","", dataset, PlotOrientation.VERTICAL, false,false,false);
        } else if ( sound.getChartType() == ChartTypes.TIME_VALUE) {
            dataset.addSeries(createSeriesTimeValue( sound.getAudio() , sound.getName() ));
            chart = ChartFactory.createXYLineChart
                    ("Dzwiek - " + sound.getName(),"","", dataset, PlotOrientation.VERTICAL, false,false,false);
        }
        return chart;
    }
    
    private static XYSeries createSeriesTimeAmp(double [] audio, String name) {
        XYSeries series = new XYSeries(name);
        for(int i=0; i<audio.length; i++) {
            series.add((float)i / 44100.0 , audio[i] * 32767);   
        }
        return series;
    }
    
    private static XYSeries createSeriesValueAmp(double [] audio, String name) {
        XYSeries series = new XYSeries(name);
        for(int i=0; i<audio.length; i++) {
            series.add(i, audio[i] * 32767);   
        }
        return series;
    }
    
    private static XYSeries createSeriesValueValue(double [] audio, String name) {
        XYSeries series = new XYSeries(name);
        for(int i=0; i<audio.length; i++) {
            series.add((float)i, audio[i]);   
        }
        return series;
    }
    
    private static XYSeries createSeriesTimeValue(double [] audio, String name) {
        XYSeries series = new XYSeries(name);
        for(int i=0; i<audio.length; i++) {
            series.add((float)i/ 44100.0 , audio[i]);   
        }
        return series;
    }
    
    private static XYSeries createSeriesFreqValue(double [] audio, String name) {
        XYSeries series = new XYSeries(name);
        for(int i=0; i<audio.length; i++) {
            series.add(44100.0 / (float)audio.length * (float) i / 2 , audio[i]);   
        }
        return series;
    }
}
